package com.yztc.administrator.meishi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yztc.administrator.meishi.R;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {

    private EditText etUserName;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化bmob
        Bmob.initialize(this,"2cd4546b029739c36ed15d8633533b56");
        //初始化控件
        init();
    }

    private void init() {
        etUserName = (EditText) findViewById(R.id.et_login_user);
        etPassword = (EditText) findViewById(R.id.et_login_password);

    }

    //登录点击事件
    public void login(View view) {
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername(etUserName.getText().toString());
        bmobUser.setPassword(etPassword.getText().toString());
        bmobUser.login(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if (e == null){
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
